package com.kele.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author kele
 * @date 2021/10/13 17:10
 * @description
 * cas 问题
 * compareAndSet 比较和交换
 */
public class CASTest {

    public static void main(String[] args) {
        AtomicInteger atomicInteger=new AtomicInteger(2021);
        /**
         * 比较和交换
         * 如果拿到了期望值那么就更新
         */
        System.out.println(atomicInteger.compareAndSet(2021, 2022));
        System.out.println(atomicInteger);

        /**
         * 比较和交换
         * 此时值已经是2022了不是期望值2021所以不会修改
         *
         */
//        System.out.println(atomicInteger.compareAndSet(2021, 2022));
//        System.out.println(atomicInteger);

        /**
         * ABA问题
         */
        System.out.println("========================捣乱的线程============================");
        System.out.println(atomicInteger.compareAndSet(2022, 2021));
        System.out.println(atomicInteger);

        System.out.println("========================希望的线程============================");
        System.out.println(atomicInteger.compareAndSet(2021, 6666));
        System.out.println(atomicInteger);


    }


}
